<?php
//Config : Les informations personnels de l'instance (log, pass, etc)
require("../include/config.php");

//API Fonctions : les fonctions fournis de base par l'API
require("../API/php/fonctions.php");

//Header établie la connection à la base $connection
require("../API/php/header.php");

//Fonctions : Fonctions personnelles de l'instance
require("../php/fonctions.php");

//Mode debug
$modeDebug = false;

//Public ou privé (clé obligatoire)
$modePublic = true;

//Mode de sortie text,json,xml,csv
//pour xml et csv $object_retour->data["resultat"] doit contenir qu'un est unique array
$modeSortie = "json";

//Liens de test
// phpsql/getIndQualiteVSR.php?milis=123450&dateSituation=2014-02-21

//Définition des entrants
$arrayInput = array(
    "dateSituation" => null
);

//Récupération des entrants
$arrayValeur = recupInput($arrayInput);

//Object retour minima
// $object_retour->strErreur string
// $object_retour->data  string
// $object_retour->statut  string
    
$connection->exec("SET CHARACTER SET utf8");

$object_retour->dateSituation = $arrayValeur["dateSituation"];

//--------------------------------------------------------------------------
$array_mois = new stdClass();
$array_mois->m3 = new stdClass();
$array_mois->m3->date = date( "Y-m", strtotime($arrayValeur["dateSituation"]." -3 month"));
$array_mois->m2 = new stdClass();
$array_mois->m2->date = date( "Y-m", strtotime($arrayValeur["dateSituation"]." -2 month"));
$array_mois->m1 = new stdClass();
$array_mois->m1->date = date( "Y-m", strtotime($arrayValeur["dateSituation"]." -1 month"));
$array_mois->m0 = new stdClass();
$array_mois->m0->date = date( "Y-m", strtotime($arrayValeur["dateSituation"]));

foreach($array_mois as $key => $value) {
    $datas = new stdClass();
    $value->data = $datas;
    $date = $value->date;

    $param = new stdClass();
    $param->dateMois=$date;

    //arriver
    $IDQ01 = getIndicateur($connection,$prefixTable, "IDQ08", 60, $param);
    $value->data->IDQ01 = intval($IDQ01->data->data["nombre"]);

    //fermer
    $IDQ02 = getIndicateur($connection,$prefixTable, "IDQ09", 60, $param);
    $value->data->IDQ02 = intval($IDQ02->data->data["nombre"]);

    //aquit
    if($value->data->IDQ02 > 0){
        $param = new stdClass();
        $param->dateMois=$date;
        $idTocs_IDQ01 = getEnumValues($IDQ01->data->data["data"],"id_toc","int");
        $param->idTocs=$idTocs_IDQ01;
        $IDQ03 = getIndicateur($connection,$prefixTable, "IDQ03", 60, $param);
        $value->data->IDQ03 = intval($IDQ03->data->data["nombre"]);
    }else{
        $value->data->IDQ03 = 0;
    }

    //out sla
    //critique
    if($value->data->IDQ02 > 0){
        $param = new stdClass();
        $idTocs_IDQ02 = getEnumValues($IDQ02->data->data["data"],"id_toc","int");
        $param->idTocs=$idTocs_IDQ02;
        $param->priorite=35;
        $IDQ07_35 = getIndicateur($connection,$prefixTable, "IDQ07", 60, $param);
        $value->data->IDQ07_35 = intval($IDQ07_35->data->data["nombre"]);
    }else{
        $value->data->IDQ07_35 = 0;
    }

    if($value->data->IDQ07_35 > 0){
        $param = new stdClass();
        $idTocs_IDQ07_35 = getEnumValues($IDQ07_35->data->data["data"],"id_toc","int");
        $param->idTocs=$idTocs_IDQ07_35;
        $param->priorite=35;
        $IDQ06_35 = getIndicateur($connection,$prefixTable, "IDQ06", 60, $param);
        $value->data->IDQ06_35 = intval($IDQ06_35->data->data["nombre"]);
    }else{
        $value->data->IDQ06_35 = 0;
    }

    //haute
    if($value->data->IDQ02 > 0){
        $param = new stdClass();
        $param->idTocs=$idTocs_IDQ02;
        $param->priorite=36;
        $IDQ07_36 = getIndicateur($connection,$prefixTable, "IDQ07", 60, $param);
        $value->data->IDQ07_36 = intval($IDQ07_36->data->data["nombre"]);
    }else{
        $value->data->IDQ07_36 = 0;
    }

    if($value->data->IDQ07_36 > 0){
        $param = new stdClass();
        $idTocs_IDQ07_36 = getEnumValues($IDQ07_36->data->data["data"],"id_toc","int");
        $param->idTocs=$idTocs_IDQ07_36;
        $param->priorite=36;
        $IDQ06_36 = getIndicateur($connection,$prefixTable, "IDQ06", 60, $param);
        $value->data->IDQ06_36 = intval($IDQ06_36->data->data["nombre"]);
    }else{
        $value->data->IDQ06_36 = 0;
    }

    //moyenne
    if($value->data->IDQ02 > 0){
        $param = new stdClass();
        $param->idTocs=$idTocs_IDQ02;
        $param->priorite=37;
        $IDQ07_37 = getIndicateur($connection,$prefixTable, "IDQ07", 60, $param);
        $value->data->IDQ07_37 = intval($IDQ07_37->data->data["nombre"]);
    }else{
        $value->data->IDQ07_37 = 0;
    }

    if($value->data->IDQ07_37 > 0){
        $param = new stdClass();
        $idTocs_IDQ07_37 = getEnumValues($IDQ07_37->data->data["data"],"id_toc","int");
        $param->idTocs=$idTocs_IDQ07_37;
        $param->priorite=37;
        $IDQ06_37= getIndicateur($connection,$prefixTable, "IDQ06", 60, $param);
        $value->data->IDQ06_37 = intval($IDQ06_37->data->data["nombre"]);
    }else{
        $value->data->IDQ06_37 = 0;
    }

    //basse
    if($value->data->IDQ02 > 0){
        $param = new stdClass();
        $param->idTocs=$idTocs_IDQ02;
        $param->priorite=38;
        $IDQ07_38 = getIndicateur($connection,$prefixTable, "IDQ07", 60, $param);
        $value->data->IDQ07_38 = intval($IDQ07_38->data->data["nombre"]);
    }else{
        $value->data->IDQ07_38 = 0;
    }

    if($value->data->IDQ07_38 > 0){
        $param = new stdClass();
        $idTocs_IDQ07_38 = getEnumValues($IDQ07_38->data->data["data"],"id_toc","int");
        $param->idTocs=$idTocs_IDQ07_38;
        $param->priorite=38;
        $IDQ06_38 = getIndicateur($connection,$prefixTable, "IDQ06", 60, $param);
        $value->data->IDQ06_38 = intval($IDQ06_38->data->data["nombre"]);
    }else{
        $value->data->IDQ06_38 = 0;
    }

    if($key == "m0"){
        //stock tout
        $param = new stdClass();
        $IDQ04 = getIndicateur($connection,$prefixTable, "IDQ10", 60, $param);
        $value->data->IDQ04 = intval($IDQ04->data->data["nombre"]);

        //stock emis
        $IDQ05 = getIndicateur($connection,$prefixTable, "IDQ11", 60, $param);
        $value->data->IDQ05 = intval($IDQ05->data->data["nombre"]);
    }
 }

$object_retour->data = $array_mois;

//--------------------------------------------------------------------------
if($modeDebug){
    $strSorti .= ($strSql);
}

//Cloture de l'interface
require("../API/php/footer.php");
?>